#include "DirectInput.h"

// sets member variables to default values
DirectInput::DirectInput()
	: m_pDIObject(NULL)
	, m_pDIKeyboard(NULL)
	, m_pDIMouse(NULL)
{
	for(int i = 0; i < 256; i++)
		m_bKeyDown[i] = false;
}

// initialize object and peripheral devices
void DirectInput::Init(HWND hWnd, HINSTANCE hInst)
{
	// create the direct input object
	DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDIObject, NULL);

	// use that object to create the keyboard and mouse devices
	m_pDIObject->CreateDevice(GUID_SysKeyboard, &m_pDIKeyboard, NULL);
	m_pDIObject->CreateDevice(GUID_SysMouse, &m_pDIMouse, NULL);

	// set data format
	m_pDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
	m_pDIMouse->SetDataFormat(&c_dfDIMouse);

	// set the cooperative level
	m_pDIKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	m_pDIMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);

	// acquire the devices
	HRESULT hr;

	hr = m_pDIKeyboard->Acquire();
	hr = m_pDIMouse->Acquire();
}

// handle input
void DirectInput::PollDevices()
{
	HRESULT hr;

	// get the most recent state of the keyboard
	hr = m_pDIKeyboard->GetDeviceState(sizeof(m_kbBuffer), (void*)&m_kbBuffer);
	if(FAILED(hr))
		return;

	if(keyDown(DIK_SPACE))
	{
		if(!m_bKeyDown[DIK_SPACE])
		{
			m_bKeyDown[DIK_SPACE] = true;
		}
	}
	else
	{
		if(m_bKeyDown[DIK_SPACE])
		{
			m_bKeyDown[DIK_SPACE] = false;
		}
	}

	if(keyDown(DIK_RETURN))
	{
		if(!m_bKeyDown[DIK_RETURN])
		{
			m_bKeyDown[DIK_RETURN] = true;
		}
	}
	else
	{
		if(m_bKeyDown[DIK_RETURN])
		{
			m_bKeyDown[DIK_RETURN] = false;
		}
	}

	// get the most recent state of the mouse
	hr = m_pDIMouse->GetDeviceState(sizeof(m_mouseState), (void*)&m_mouseState);

	if(FAILED(hr))
		return;

}

bool DirectInput::keyDown(unsigned char key)
{
	return (m_kbBuffer[key] & 0x80) != 0;
}

bool DirectInput::mouseButtonDown(int button)
{
	return(m_mouseState.rgbButtons[button] & 0x80) != 0;
}

// release memory
DirectInput::~DirectInput()
{
	m_pDIKeyboard->Unacquire();
	m_pDIMouse->Unacquire();

	m_pDIObject->Release();
	m_pDIKeyboard->Release();
	m_pDIMouse->Release();
}